import { Component, OnInit, Input, ViewChild } from '@angular/core';
import { NgbActiveModal } from '@ng-bootstrap/ng-bootstrap';
import { BasicService } from '../../../../@core/data/basic.service';
import { ServerDataSource } from 'ng2-smart-table';
// 消息提醒
import { ToasterConfig } from 'angular2-toaster';
import { NotificationsService } from '../../../../@core/utils/notifications.service';
import { FroalaComponent } from '../../../../@theme/components/index';
import { PreviewqComponent } from '../../previewq/previewq.component';
import { BsDatepickerConfig, BsLocaleService } from 'ngx-bootstrap/datepicker';
import { listLocales } from 'ngx-bootstrap/bs-moment';
const api = 'papers';
@Component({
  selector: 'ngx-paper-modal',
  templateUrl: './paper.modal.component.html',
})
export class PaperModalComponent implements OnInit {

  @Input() modalHeader: string;
  @Input() isEdit: boolean;
  // isEdit = true 需要paperId去查询detail
  @Input() paperId: number;
  @Input() paper: any;
  @Input() source: ServerDataSource;

  statusType: any[];
  courseType: any[];
  qbankType: any[];
  @Input() choiceHidden: boolean;
  previewHidden: boolean;
  locale = 'zh-cn';

  constructor(private activeModal: NgbActiveModal, private basicService: BasicService,
    private notificationsService: NotificationsService, private localeService: BsLocaleService) {
  }

  ngOnInit() {
    // 初始化下拉框的数据
    this.basicService.getOne('courses/findAll')
      .subscribe((data: any) => {
        this.courseType = data.rows;
      });
    this.basicService.getOne('configs/findByCtype?ctype=status_type')
      .subscribe((data: any) => {
        this.statusType = data.rows;
      });
    if (this.isEdit && this.paper.courseId) {
      this.basicService.getOne('qbanks/findByCourseId?courseId=' + this.paper.courseId)
        .subscribe((data: any) => {
          this.qbankType = data.rows || [];
        });
    } else {
      this.basicService.getOne('qbanks/findAll')
        .subscribe((data: any) => {
          this.qbankType = data.rows;
        });
    }
    // 编辑需要初始化日期
    if (this.isEdit) {
      if (this.paper.startTime && this.paper.endTime) {
        this.paper.termOfValidity = [new Date(this.paper.startTime), new Date(this.paper.endTime)];
        // this.paper.termOfValidity = [this.paper.startTime, this.paper.endTime];
      }
    }
    this.localeService.use(this.locale);
  }

  // 取消
  closeModal() {
    this.activeModal.close();
  }

  // 保存
  saveData() {
    if (this.isEdit) {
      this.paper.content = JSON.stringify(this.paper.content);
      if (this.paper.termOfValidity) {
        this.paper.startTime = this.paper.termOfValidity[0];
        this.paper.endTime = this.paper.termOfValidity[1];
      }
      this.basicService.update(api, this.paper).subscribe(
        data => {
          this.notificationsService.showToast('success', '操作', '<b>更新 <em>成功！</em></b>');
          this.activeModal.close();
          this.source.refresh();
        },
        err => {
          this.notificationsService.showToast('error', '操作', '<b>更新 <em>失败！</em></b>');
          console.error('Error saving!');
        },
      );
    } else {
      this.paper.content = JSON.stringify(this.paper.content);
      if (this.paper.termOfValidity) {
        this.paper.startTime = this.paper.termOfValidity[0];
        this.paper.endTime = this.paper.termOfValidity[1];
      }
      this.basicService.create(api, this.paper).subscribe(
        data => {
          this.notificationsService.showToast('success', '操作', '<b>保存 <em>成功！</em></b>');
          this.activeModal.close();
          this.source.refresh();
        },
        err => {
          this.notificationsService.showToast('error', '操作', '<b>保存 <em>失败！</em></b>');
          console.error('Error saving!');
        },
      );
    }
  }

  onCourseChange(event) {
    const courseId = event.target.value;
    this.basicService.getOne('qbanks/findByCourseId?courseId=' + courseId)
      .subscribe((data: any) => {
        const rows = data.rows || [];
        const emptyOption = {
          id: '',
          title: '请选择题库',
        };
        rows.unshift(emptyOption);
        this.qbankType = rows;
      });
  }

}
